package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/15.
 */
public class XingXingBangAdatper extends BaseAdapter {
    private Context mContext;
    private List<EatShowUserInfo> mList = new ArrayList<EatShowUserInfo>();

    public XingXingBangAdatper(Context context) {
        mContext = context;
    }
    public void updateData(List<EatShowUserInfo> list){
        if(mList != null){
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData(){
        if(mList != null){
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mList != null){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mList != null){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if(convertView == null || convertView.getTag()== null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.xingxing_item,null);
            childHolder.zhuboImg = (ImageView) convertView.findViewById(R.id.xingxing_zhubo_icon);
            childHolder.zhuboNick = (TextView) convertView.findViewById(R.id.xingxing_zhubo_nick);
            childHolder.zhuboSign = (TextView) convertView.findViewById(R.id.xingxing_zhubo_sign);
            childHolder.fansNum = (TextView) convertView.findViewById(R.id.xingxing_zhubo_fansnum);
            childHolder.paiming = (TextView) convertView.findViewById(R.id.xingxing_num);

            convertView.setTag(childHolder);
        }else{
            childHolder = (ChildHolder) convertView.getTag();
        }

        EatShowUserInfo info = mList.get(position);
        if(info != null){
            String anchorImgUrl = info.getUserLogo();
            if(anchorImgUrl != null && anchorImgUrl.length()>0){
                ViewUtil.Size size =  ViewUtil.getHeadImageSize_40(mContext);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(anchorImgUrl,size));
                if (cacheBitmap != null){
                    childHolder.zhuboImg.setImageBitmap(cacheBitmap);
                }else {
                    childHolder.zhuboImg.setImageResource(R.drawable.default_head_image);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.zhuboImg, anchorImgUrl, size);
                }
            }
//           childHolder.zhuboNick.setText(info.getNickName());
//            childHolder.zhuboSign.setText(info.getUserSign());
            if(info.getNickName()!=null){
                try {
                    childHolder.zhuboNick.setText(URLDecoder.decode(info.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if(info.getUserSign()!=null){
                try {
                    childHolder.zhuboSign.setText(URLDecoder.decode(info.getUserSign(),"utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            childHolder.fansNum.setText("粉丝："+info.getFansNum());
            int ph =position+1;
//            if(ph==1){
//                childHolder.paiming.setBackgroundResource(R.drawable.top1);
//                childHolder.paiming.setText("");
//            }else if(ph==2){
//                childHolder.paiming.setBackgroundResource(R.drawable.top2);
//                childHolder.paiming.setText("");
//            }else if(ph==3){
//                childHolder.paiming.setBackgroundResource(R.drawable.top3);
//                childHolder.paiming.setText("");
//            }else{
//                childHolder.paiming.setBackgroundResource(0);
                childHolder.paiming.setText(ph+"");
//            }

        }



        return convertView;
    }

    class ChildHolder{
        ImageView zhuboImg;
        TextView zhuboNick;
        TextView zhuboSign;
        TextView fansNum;
        TextView paiming;

    }
}
